package com.pks.autoInsurance.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.pks.autoInsurance.dto.CustomerDto;
import com.pks.autoInsurance.helper.RegistrationHelper;
import com.pks.autoInsurance.helper.UserHelper;

/**
 * User related operations Servlet implementation class
 * 
 * @author Pankaj Soni
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(UserServlet.class);

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOGGER.debug("begin");
		String task = request.getParameter("task");
		LOGGER.info("task: " + task);
		if ("profile-update".equals(task)) {
			String ssn = (String) request.getSession(false).getAttribute("ssn");
			CustomerDto userProfile = new UserHelper().getUserProfile(ssn);
			request.setAttribute("userProfile", userProfile);
			request.getRequestDispatcher("profileUpdate.jsp").forward(request,
					response);
			return;
		} else {
			LOGGER.debug("not relavent task");
		}
		LOGGER.debug("end");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOGGER.debug("begin");
		String task = request.getParameter("task");
		LOGGER.info("task: " + task);
		if ("profile-update".equals(task)) {
			this.userProfileUpdate(request, response);
		}
		LOGGER.debug("end");
	}

	private void userProfileUpdate(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			LOGGER.debug("begin");
			String ssn = request.getParameter("ssn");
			String passwd = request.getParameter("passwd");
			String firstName = request.getParameter("fName");
			String lastName = request.getParameter("lName");
			String gender = request.getParameter("gender");
			String mobileNo = request.getParameter("mobileNo");
			String emailId = request.getParameter("email");
			String city = request.getParameter("city");
			String bloodGroup = request.getParameter("bloodGroup");
			String drivingLicence = request.getParameter("drivingLicence");
			String dateOfBirthString = request.getParameter("dateOfBirth");
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd");
			java.util.Date dateOfBirth = null;
			try {
				dateOfBirth = dateFormat.parse(dateOfBirthString);
			} catch (ParseException e) {
				LOGGER.error(e.getMessage());
			}

			CustomerDto regDto = new CustomerDto();
			regDto.setSsn(ssn);
			regDto.setPasswd(passwd);
			regDto.setFirstName(firstName);
			regDto.setLastName(lastName);
			regDto.setGender(gender);
			regDto.setDateOfBirth(dateOfBirth);
			regDto.setMobileNo(mobileNo);
			regDto.setEmail(emailId);
			regDto.setCity(city);
			regDto.setBloodGroup(bloodGroup);
			regDto.setDrivingLicence(drivingLicence);
			LOGGER.info("registrationDto: " + regDto);
			boolean isUserUpdated = new RegistrationHelper().updateUser(regDto);
			LOGGER.info("isUserUpdated: " + isUserUpdated);

			request.getRequestDispatcher("home.jsp").forward(request, response);
		} catch (ServletException e) {
			LOGGER.error(e.getMessage());
		} catch (IOException e) {
			LOGGER.error(e.getMessage());
		}
		LOGGER.debug("end");

	}

}
